#ifndef ICESTREAM_H
#define ICESTREAM_H

#include "nice/nice.h"
#include "icecandidate.h"
#include <iostream>
#include <vector>
#include <cstring>
#include <pthread.h>
#include  <signal.h>

typedef void (*receive_callback)( guint size, gchar* data, guint component_id );

class ICEStream
{
public:
    ICEStream( NiceAgent* agent, guint stream_id, guint n_component );
    ~ICEStream();
    std::vector<ICECandidate> getLocalCandidatesFromComponent( guint component_id = 1 );
    std::vector<ICECandidate> getLocalCandidates();
    void setRemoteCandidates( std::vector<ICECandidate> rcands );
    void send( guint size, gchar* data, guint component_id = 1);
    void receive( guint size, gchar* data, guint component_id );
    void candidateGatheringDone();
    void newSelectedPair();
    std::string getLocalUsername();
    std::string getLocalPassword();
    void setRemoteCredentials( std::string remote_username, std::string remote_password );
    void setReceiveCallback( receive_callback rcv_cb );
    void release();
    void timeout();

    bool timeout_error;

    static ICEStream* alarm_icestrm;

private:
    NiceAgent* agent;
    guint stream_id;
    guint n_component;
    std::string local_username;
    std::string local_password;
    pthread_mutex_t gathering_candidates_mutex;
    pthread_mutex_t selecting_pair_mutex;
    std::string candidate_protocol[1];
    std::string candidate_type[4];
    receive_callback rcv_cb;

    void freeCandidatesGSList( GSList* cands );
    std::vector<ICECandidate> niceCandidateToICECandidate( GSList* cands );
    std::vector<GSList*> iceCandidateToNiceCandidate( std::vector<ICECandidate> cands );
};

#endif // ICESTREAM_H
